package HW2_1.Firefox_DriverFactory;

import java.util.regex.Pattern;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

import ru.esteru.selenium.factory.WebDriverFactory;



public class c_delete_user {
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	
	
	
	@Before
	public void setUp() throws Exception {
		 
		 ProfilesIni allProfiles = new ProfilesIni(); 
	     FirefoxProfile profile = allProfiles.getProfile("default"); 
	     driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
	     baseUrl = "http://localhost/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}
	

	
	@Test
	public void b_delete_user() throws Exception {
		GoToMainPage();
		loginAs("admin", "admin");
		userDelete();
		logout();
	}
	

	private void logout() {
		driver.findElement(By.linkText("Log out")).click();
		driver.switchTo().alert().accept();
	}

	
	private void userDelete() {
		driver.findElement(By.linkText("User management")).click();
		driver.findElement(By.cssSelector("img[alt=\"Remove\"]")).click();
		driver.switchTo().alert().accept();
	}
	

	private void loginAs(String username, String password) {
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(username);
		driver.findElement(By.name("password")).clear();
		driver.findElement(By.name("password")).sendKeys(password);
		driver.findElement(By.name("submit")).click();
	}
	

	private void GoToMainPage() {
		driver.get(baseUrl + "/Tools/php4dvd/");
	}

	
	
	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
